
/** 封装的请求类 */
function parseJSON(response){
    return response.json()
}

function checkStatus(response){
    if(response.status >= 200 && response.status < 500){
        return response
    }
    const error = new Error(response.statusText)
    error.response = response
    throw error
}

/**
 * 普通请求
 * @param {*url,*method,*data} options
 */
export  function request(options = {}){
    const Authorization = localStorage.getItem('x-access-token')
    const {data,url} = options
    options = {...options}
    options.mode = 'cors'
    delete options.url
    if(data){
        delete options.data
        options.body = data
    }
    options.headers={
        'x-access-token':Authorization,
        'Content-Type':'application/json;charset=UTF-8'
    }
    return fetch(url,options,{credentials: 'include'})
        .then(checkStatus)
        .then(parseJSON)
        .catch(err=>({err}))
}